<template>
  <div class="bottom-bar">
    <div class="check-content">
      <cart-check-button :is-checked="isSelectAll"
                         class="check-button"
                         @click.native="checkClick"/>
      <span>全选</span>
    </div>
    <div class="price">
      合计：￥{{totalPrice}}
    </div>
    <div class="calculate">
      去结算({{checkLength}})
    </div>
  </div>
</template>

<script>
import CartCheckButton from './CartCheckButton'

export default {
  name: "CartBottomBar",
  components: {
    CartCheckButton
  },
  computed: {
    totalPrice() {
      return this.$store.getters.cartList.filter(item => {
        return item.checked
      }).reduce((preValue, item) => {
        return preValue + item.price * item.count
      }, 0)
    },
    checkLength() {
      return this.$store.getters.cartList.filter(item => item.checked).length
    },
    isSelectAll() {
      // if (this.$store.getters.cartList.length === 0) return false
      // return !(this.$store.getters.cartList.filter(item => item.checked).length)
      // return !this.$store.state.cartList.find(item => item.checked)
      for (let item of this.$store.getters.cartList) {
        if (!item.checked) {
          return false
        }
      }
      return true
    }
  },
  methods: {
    checkClick() {
      if (this.isSelectAll) {
        this.$store.getters.cartList.forEach(item => item.checked = false)
      } else {
        this.$store.getters.cartList.forEach(item => item.checked = true)
      }
    }
  }
}
</script>

<style scoped>
.bottom-bar {
  display: flex;
  height: 40px;
  background-color: #eee;
  position: relative;
  line-height: 40px;
}

.check-content {
  display: flex;
  align-items: center;
  margin-left: 10px;
}

.check-button {
  width: 20px;
  height: 20px;
  line-height: 20px;
  margin-right: 5px;
}

.price {
  margin-left: 20px;
  flex: 1;
}

.calculate {
  width: 90px;
  background: red;
  color: #fff;
  text-align: center;
}
</style>
